package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.Serializable;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelMusicAdapter;
import flc.ast.databinding.ActivitySelMusicBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.media.MediaLoader;
import stark.common.basic.media.bean.AudioBean;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelMusicActivity extends BaseAc<ActivitySelMusicBinding> {

    private SelMusicAdapter musicAdapter;
    private int oldPosition;
    private String selPath="";

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_music;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivSelMusicBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.ivNext.setOnClickListener(this);
        mDataBinding.rvSelMusicList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        musicAdapter = new SelMusicAdapter();
        mDataBinding.rvSelMusicList.setAdapter(musicAdapter);
        musicAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivNext://下一步
                if(selPath.isEmpty()){
                    ToastUtils.showShort(getString(R.string.please_sel_music));
                    return;
                }
                //更换图片
                Intent intent = new Intent();
                intent.putExtra(Content.musicPath, selPath);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        musicAdapter.getItem(oldPosition).setSelected(false);
        musicAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        musicAdapter.getItem(position).setSelected(true);
        musicAdapter.notifyItemChanged(position);
        selPath = musicAdapter.getItem(position).getUri();
    }

    @Override
    protected void initData() {
        getPermission();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_AUDIO)
                .reqPermissionDesc(getString(R.string.get_music_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        ToastUtils.showShort(R.string.no_permission);
                    }
                }).request();
    }

    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<AudioBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<AudioBean>> emitter) {
                List<AudioBean> audioBeans = MediaLoader.loadAudio();
                emitter.onNext(audioBeans);
            }

            @Override
            public void accept(List<AudioBean> audioBeanList) {
                if (audioBeanList != null) {
                    musicAdapter.setList(audioBeanList);
                    mDataBinding.rvSelMusicList.setVisibility(View.VISIBLE);
                    mDataBinding.tvSelMusicNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSelMusicList.setVisibility(View.GONE);
                    mDataBinding.tvSelMusicNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}